package com.lanswon.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.entity.Activity;
import com.lanswon.service.ActivityService;
import com.lanswon.service.UserService;

@Controller
@RequestMapping("front/activity")
public class ActivityController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private ActivityService activityService;
	
	/**
	 * @param currentPage
	 * @param pageNum
	 * @param type
	 * @param articleStatus
	 * @return
	 */
	@RequestMapping("/pageList")
	@ResponseBody
	public Map<String, Object> pageList(HttpServletRequest request, HttpServletResponse response,
			Integer currentPage,Integer pageNum,Integer activityType,
			Integer activityStatus){
		Map<String, Object> viewMap = new HashMap<>();
		Map<String, Object> accessMap = new HashMap<>();
		List<Activity> articleList = null;
		int code = 0;
		String message = "查询成功";
		try {
			if(currentPage==null) {
				currentPage = 1;
			}
			if(pageNum==null) {
				pageNum = 10;
			}
			accessMap.put("activityType", activityType);
			accessMap.put("activityStatus", activityStatus);
			accessMap.put("currentResult", (currentPage-1)*pageNum);
			accessMap.put("endResult", pageNum);
			// 验证token
			/*Map<String,String > checkMap=userService.checkToken(token,String.valueOf(userId));
			if ("false".equals(checkMap.get("flag"))) {
				resultMap.put("code", 1);
				resultMap.put("message", checkMap.get("message"));
				return resultMap;
			}*/
			articleList = activityService.pageList(accessMap);
			
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 2);
			viewMap.put("message", "操作有误");
		}
		viewMap.put("data", articleList);
		viewMap.put("code", code);
		viewMap.put("message", message);
		return viewMap;
	}
	
	/**
	 * 文章详情页
	 * @param articleId
	 * @param articleType
	 * @return
	 */
	@RequestMapping("detail")
	@ResponseBody
	public Map<String, Object> articleDetail(HttpServletRequest request,Long id) {
		Map<String, Object> viewMap = new HashMap<>();
		Map<String, Object> accessMap = new HashMap<>();
		List<Activity> activityList = new ArrayList<>();
		int code = 0;
		String message = "查询成功";
		try {
			if(id!=null) {
				accessMap.put("id", id);
				Activity article = activityService.findArticleById(id);
				activityList.add(article);
			}
		} catch (Exception e) {
			e.printStackTrace();
			message = "查询失败";
		}
		viewMap.put("data", activityList);
		viewMap.put("code", code);
		viewMap.put("message", message);
		return viewMap;
	}
	
	
}
